package quickstart.action;

import com.alibaba.fastjson.JSONObject;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;
import org.apache.struts2.ServletActionContext;
import org.springframework.dao.DataAccessException;
import quickstart.model.Person;
import quickstart.service.PersonService;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by feilin on 2017/1/31.
 */
public class PersonAjaxAction implements Preparable {


    private PersonService service;
    private List<Person> persons;

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    private Person person;
    private Integer id;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }

    private String json;

    public PersonAjaxAction(PersonService service) {
        this.service = service;
    }

    public void prepare() throws Exception {
        if (id != null)
            person = service.find(id);
    }

    public void grid() {

        Map dataMap = new HashMap();
        List dataList = new ArrayList();
        dataMap.put("Rows", dataList);

        this.persons = service.findAll();

        for (Person p : persons) {
            Map map = new HashMap();
            dataList.add(map);

            map.put("id", p.getId());
            map.put("firstName", p.getFirstName());
            map.put("lastName", p.getLastName());
            map.put("sex", p.getSex());
        }



        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/x-json; charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");

        JSONObject object = (JSONObject) JSONObject.toJSON(dataMap);
        try {
            response.getWriter().print(object.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void upload() {
        Map map = new HashMap();

        JSONObject object = JSONObject.parseObject(json);
        if (object != null) {
            String firstName = object.getString("firstName");
            String lastName = object.getString("lastName");
            Boolean sex = !object.getBoolean("sex");


            Person person = new Person();
            person.setLastName(lastName);
            person.setFirstName(firstName);
            person.setSex(sex);


            try {
                service.save(person);
                map.put("code", 200);
            } catch (DataAccessException e) {
                map.put("code", 201);
            }
        } else {
            map.put("code", 300);
        }

        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/x-json; charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");

        JSONObject obj = (JSONObject) JSONObject.toJSON(map);
        try {
            response.getWriter().print(obj.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void delete() {
        Map map = new HashMap();
        if (id != null) {
            service.remove(id);
            map.put("code", 200);
        } else {
            map.put("code", 300);
        }

        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/x-json; charset=UTF-8");
        response.setHeader("Cache-Control", "no-cache");

        JSONObject obj = (JSONObject) JSONObject.toJSON(map);
        try {
            response.getWriter().print(obj.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String update() { //修改前
        person = service.findById(id);
        return Action.SUCCESS;
    }

    public List<Person> getPersons() {
        return persons;
    }

    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }
}
